package com.woniuxy.project.send.config.topic;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class RabbitTopicConfig {

    /**
     * 创建并配置一个直连交换机(DirectExchange)
     * 该交换机用于消息的精确路由，根据routing key完全匹配来转发消息
     * 交换机名称为"direct-exchange"，具有持久化特性
     *
     * @return 配置好的DirectExchange实例
     */
    @Bean
    public DirectExchange directExchange() {
        // 交换机持久化：true
        return new DirectExchange("direct-exchange");
    }

    @Bean
    public Queue queueDirect1() {
        // 队列持久化配置：第二个参数设为true，确保消息在服务器重启后不会丢失
        return new Queue("queue1-direct", true);
    }

    @Bean
    public Queue queueDirect2() {
        // 队列持久化配置：第二个参数设为true，确保消息在服务器重启后不会丢失
        return new Queue("queue2-direct", true);
    }

    /*
     * 创建 Direct 交换机
     * 1、创建一个交换机：direct-exchange
     * 2、创建两个队列：queue1-direct, queue2-direct
     * 3、将两个队列绑定到交换机    路由key
     *      queue1-direct       a1
     *      queue2-direct       a2
     */

    @Bean
    public Binding directBinding1(Queue queueDirect1, DirectExchange directExchange) {
        return BindingBuilder.bind(queueDirect1).to(directExchange).with("a1");
    }

    @Bean
    public Binding directBinding2(Queue queueDirect2, DirectExchange directExchange) {
        return BindingBuilder.bind(queueDirect2).to(directExchange).with("a2");
    }

}
